package top.yangguangmc.smart_scheduling.utils;

import org.jetbrains.annotations.Contract;

import java.awt.*;

public class Utils {
    @Contract(pure = true)
    public static Point centerXY(int originalWidth, int originalHeight) {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int xCenter = d.width / 2;
        int yCenter = d.height / 2;
        return new Point(xCenter - (originalWidth / 2), yCenter - (originalHeight / 2));
    }

    @Contract(pure = true)
    public static Point centerXY(Dimension dim) {
        return centerXY(dim.width, dim.height);
    }

    @Contract(pure = true)
    public static Point centerXY(int originalWidth, int originalHeight, Window fromWindow) {
        if (fromWindow == null) return centerXY(originalWidth, originalHeight);
        else {
            int x = fromWindow.getX();
            int y = fromWindow.getY();
            int width = fromWindow.getWidth();
            int height = fromWindow.getHeight();

            int xCenter = x + (width / 2);
            int yCenter = y + (height / 2);

            int resultX = xCenter - (originalWidth / 2);
            int resultY = yCenter - (originalHeight / 2);
            return new Point(resultX,resultY);
        }
    }

    @Contract(value = "null -> true", pure = true)
    public static boolean isNullOrEmpty(Object obj) {
        if (obj == null) return true;
        if (obj instanceof String) {
            String s = (String) obj;
            s = s.replaceAll("\\s", "");
            return s.isEmpty();
        }
        return false;
    }
}
